package com.crowdfunding.controller;


import com.crowdfunding.entity.RecordCrowdFunding;
import com.crowdfunding.service.IRecordCrowdFundingService;
import com.crowdfunding.util.JwtUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoyu
 * @since 2021-07-14
 */
@RestController
@CrossOrigin
@RequestMapping("/crowdfunding/recordCrowdFunding")
public class RecordCrowdFundingController {

    @Autowired
    IRecordCrowdFundingService recordCrowdFundingService;

    @GetMapping("/recordCrowdFundingList")
    @ApiOperation("根据众筹id获取参与该众筹的用户记录列表")
    @ResponseBody
    public List<RecordCrowdFunding> getRecordCrowdFundinglist(@ApiParam("项目id") String cfId){

        return recordCrowdFundingService.getRecordList(cfId);
    }

    @GetMapping("/getRecordTotalAmount")
    @ResponseBody
    @ApiOperation("查询该用户在该众筹中所筹总金额")
    public ResponseEntity getRecordTotalAount(@RequestHeader String token ,String cfId) {
        if (JwtUtils.checkToken(token)) {
            String username = JwtUtils.getUserNameByJwtToken(token);
            double totalAmount = recordCrowdFundingService.getRecordTotalAmount(username, cfId);
            return ResponseEntity.status(HttpStatus.OK).body(totalAmount);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body("身份验证失败");
        }
    }
}

